#include "FotoIdObject.h"
int FotoIdObject::fotoid = 0;
int FotoIdObject::disab_name = 0;
int FotoIdObject::foto_list_name = 0;
int FotoIdObject::positioning_name = 0;
bool FotoIdObject::compiled = false;

FotoIdObject::FotoIdObject(){}
FotoIdObject::~FotoIdObject() {}

void FotoIdObject::LoadLists()
{

//assegno i nomi alle liste
foto_list_name = glGenLists(3);
positioning_name = foto_list_name+1;
disab_name = positioning_name +1;

//posiziono la foto e abilito il blending
glNewList(positioning_name,GL_COMPILE);
    glTranslatef(-4.5f,12.0f,24);
    glRotatef(-26.0f,1,0,0);
    glTranslatef(1.9,-3.5,0);
    glColor4f(0.50f, 1.0f, 1.0f, 0.95);     
    glBlendFunc(GL_ONE,GL_ONE);       
    glEnable(GL_BLEND);         
    glEnable(GL_TEXTURE_2D);
glEndList();

//disegno la mappa
glNewList(foto_list_name,GL_COMPILE);
        glBegin(GL_QUADS);
          glTexCoord2f(0.0f, 1.0f);
          glVertex3f(0, 0,0);
          glTexCoord2f(0.0f, 0.0f);
          glVertex3f(0, 1,0);
           glTexCoord2f(1.0f, 0.0f);
          glVertex3f(1, 1,0);
          glTexCoord2f(1.0f, 1.0f);
          glVertex3f(1, 0,0);
        glEnd();
glEndList();

//disabilito le cose settate nella disply list
glNewList(disab_name,GL_COMPILE);
        glDisable(GL_TEXTURE_2D);
        glDisable(GL_BLEND);
glEndList();
compiled = true;
}
void FotoIdObject::DoIdle()
{
    time_t t2;
    time(&t2);
    
    double diff = difftime(t2,tempo);
    
    //cambio la foto
    if ( diff >= 1)
      {
       if (fotoid == 6 || fotoid == 8 || fotoid == 9)  fotoid = 5;  
       else if (fotoid == 7 || fotoid == 8 || fotoid == 9)  fotoid = 5;
       else if (fotoid == 5)  fotoid = int(rand()% 2) + 6;
       
       time(&tempo);  
      // printf("\n foto %d",fotoid);
      }
      
       
}
void FotoIdObject::Init()
{
    fotoid = 5;
    visible = true;
    time(&tempo);
    if (!compiled) LoadLists();
}
void FotoIdObject::Draw(){
   
if (visible){ 
glPushMatrix();
glLoadIdentity();
        //posiziono la foto
        glCallList(positioning_name);
       
        //assegno la texture variabile
        glBindTexture(GL_TEXTURE_2D,state->th->texNames[fotoid]);
        
        //disegno la foto
        glCallList(foto_list_name); 
        glCallList(disab_name); 
glPopMatrix();
}
}

